package com.xujiang.common.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.xujiang.common.R;

/**
 * 界面工具
 *
 * @author xujiang
 * @version 2017-05-04 11:10
 */
public class UIUtils {
    public static int LENGTH_LONG = Toast.LENGTH_LONG;
    public static int LENGTH_SHORT = Toast.LENGTH_SHORT;
    public static int SNACK_LENGTH_SHORT = Snackbar.LENGTH_SHORT;
    public static int SNACK_LENGTH_LONG = Snackbar.LENGTH_LONG;
    public static int SNACK_LENGTH_INDEFINITE = Snackbar.LENGTH_INDEFINITE;  //不取消  手动取消

    private static Toast mToast;
    private static Snackbar msnackbar;

    /**
     * 吐丝提示
     *
     * @author xujiang
     * @time 2017/5/4 13:43
     */
    public static void showToast(@NonNull Context context, String hint) {
        showToast(context, hint, LENGTH_LONG);
    }

    /**
     * 显示吐丝
     *
     * @param context 上下文对象
     * @param hint 消息体
     * @param duration 显示时长
     * @author xujiang
     * @time 2017/5/4 13:43
     */
    public static void showToast(@NonNull Context context, String hint, int duration) {
        showToast(context, hint, duration, Gravity.CENTER, 0, 0);
    }

    /**
     * 显示吐丝 可设置位置
     *
     * @param gravity 设置显示位置
     * @param xOffset 设置x轴偏移
     * @param yOffset 设置y轴偏移
     */
    public static void showToast(@NonNull Context context, String hint, int duration, int gravity, int xOffset,
            int yOffset) {
        if (mToast == null) { //mToast对象的重用
            mToast = Toast.makeText(context, hint, duration);
            mToast.setGravity(gravity, xOffset, yOffset);  //设置通知应该出现在屏幕上的位置。
        } else {
            mToast.setText(hint);  //直接设置内容
        }
        mToast.show();
    }

    /**
     * 吐丝提示与log
     */
    public static void showLogToast(String TAG, @NonNull Context context, String hint,
            int duration) {
        showToast(context, hint, duration);
        //L.i(TAG, hint);
    }

    /**
     * 吐丝提示与log
     */
    public static void showLogToast(String TAG, @NonNull Context context, String hint) {
        showToast(context, hint);
        //L.i(TAG, hint);
    }

    /**
     * 吐丝提示与log
     */
    public static void showLogToast(String TAG, @NonNull Context context, String hint,
            int duration, int gravity, int xOffset, int yOffset) {
        showToast(context, hint, duration, gravity, xOffset, yOffset);
        //L.i(TAG, hint);
    }

    /**
     * 清除mToast变量
     */
    public static void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
        }
    }

    /**
     * snack start
     *
     * 说明  使用以下snack时用CoordinatorLayout作为布局在xml中  主要是为了实现右划隐藏布局，否则调用无效
     * 官方推荐使用CoordinatorLayout来确保Snackbar和其他组件的交互
     *
     */

    /**
     * @param activity 上下文对象  本质需要一个view
     * @param hint 要显示的消息
     */
    public static void showSnack(@NonNull Activity activity, String hint) {
        showSnack(activity, hint, SNACK_LENGTH_LONG);
    }

    /**
     * @param hint 一个string的资源
     */
    public static void showSnack(@NonNull Activity activity, @StringRes int hint) {
        showSnack(activity, activity.getResources().getString(hint));
    }

    /**
     * R.string.ek_cc_ok = OK
     *
     * @param activity 显示依赖的view
     * @param hint 显示类容
     * @param duration 显示时长
     */
    public static void showSnack(@NonNull final Activity activity, final String hint,
            final int duration) {
        showSnack(activity, hint, duration, activity.getText(R.string.common_ok).toString(), null, 0, 0, 0);
    }

    /**
     * 具体实现 activity
     *
     * @param action 为snakebar上的一个可点击按钮
     * @param callback2 回掉
     * http://www.jianshu.com/p/cd1e80e64311
     */
    public static void showSnack(@NonNull Activity activity, final String hint, final String action,
            final Callback2<View> callback2) {
        showSnack(activity, hint, SNACK_LENGTH_LONG, action, callback2, 0, 0, 0);
    }

    public static void showSnack(@NonNull Activity activity, final String hint, final int duration,
            final String action, final Callback2<View> callback2, @DrawableRes final int backgroundcolor, @ColorInt
    final int actiontextcolor, @ColorInt final int textcolor) {
        getCoordinatorLayout(activity, new Callback4.EmptyCallback<View>() {
            @Override
            public void onYes(View view) {
                msnackbar = Snackbar.make(view, hint, duration);
                if (backgroundcolor != 0) {
                    setBackGroundResource(backgroundcolor);
                }
                if (actiontextcolor != 0) {
                    setActionTextColor(actiontextcolor);
                }
                if (textcolor != 0) {
                    setActionTextColor(textcolor);
                }

                msnackbar.setAction(action, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (callback2 == null) {
                            return;
                        }
                        callback2.onSuccess(v);
                    }
                }).show();
            }
        });
    }

    public static Snackbar setBackGroundResource(@DrawableRes int resid) {
        //设置背景颜色
        msnackbar.getView().setBackgroundResource(R.color.white);
        return msnackbar;
    }

    public static Snackbar setActionTextColor(@ColorInt int color) {
        //设置按钮文字颜色
        msnackbar.setActionTextColor(Color.WHITE);
        return msnackbar;
    }

    public Snackbar setTextColor(View view, @ColorInt int color) {
        //获取Snackbar的message控件，修改字体颜色
        ((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(Color.BLACK);
        return msnackbar;
    }

    /**
     * 以下传入的是view
     */
    public static void showSnack(@NonNull View view, String hint) {
        showSnack(view, hint, SNACK_LENGTH_LONG);
    }

    public static void showSnack(@NonNull View view, final String hint, final int duration) {
        showSnack(view, hint, duration, view.getResources().getText(R.string.common_ok).toString(),
                null, 0, 0, 0);
    }

    public static void showSnack(@NonNull View view, final String hint, final String action,
            final Callback2<View> callback2) {
        showSnack(view, hint, SNACK_LENGTH_LONG, action, callback2, 0, 0, 0);
    }

    public static void showSnack(@NonNull View view, final String hint, final int duration,
            final String action, final Callback2<View> callback2, @DrawableRes final int backgroundcolor, @ColorInt
    final int actiontextcolor, @ColorInt final int textcolor) {
        getCoordinatorLayout(view, new Callback4.EmptyCallback<View>() {
            @Override
            public void onYes(View view) {
                msnackbar = Snackbar.make(view, hint, duration);
                if (backgroundcolor != 0) {
                    setBackGroundResource(backgroundcolor);
                }
                if (actiontextcolor != 0) {
                    setActionTextColor(actiontextcolor);
                }
                if (textcolor != 0) {
                    setActionTextColor(textcolor);
                }
                msnackbar.setAction(action, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (callback2 == null) {
                            return;
                        }
                        callback2.onSuccess(v);
                    }
                }).show();
            }
        });
    }

    /**
     * snake 和log
     */
    public static void showLogSnack(String TAG, @NonNull View view, String hint, int duration) {
        showSnack(view, hint, duration);
        //L.i(TAG, hint);
    }

    public static void showLogSnack(String TAG, @NonNull final Activity activity, final String hint,
            final int duration) {
        showSnack(activity, hint, duration);
        //L.i(TAG, hint);
    }

    public static void showLogSnack(String TAG, @NonNull View view, String hint) {
        showSnack(view, hint);
        // L.i(TAG, hint);
    }

    public static void showLogSnack(String TAG, @NonNull Activity activity, final String hint) {
        showSnack(activity, hint);
        // L.i(TAG, hint);
    }

    /**  Snack end **/

    /**
     * findViewById方法的顶层可能是view 也可能是viewgroup
     * 但我个人觉得没必要用viewgroup承载 可能是特殊情况需要
     * http://www.jianshu.com/p/afa921d8ed24  android.R.id.content
     */
    public static View getActivityContentView(Activity activity) {
        if (activity == null) {
            return null;
        }
        ViewGroup group = findViewById(activity, android.R.id.content);//group是得到的ContentFrameLayout id是content
        return group.getChildAt(0);  //得到view返回 其实是获得一个根视图，在xml的最外层
    }

    /**
     * 得到CoordinatorLayout
     *
     * @param activity 一个activity
     * @param call 回掉函数
     */
    public static void getCoordinatorLayout(Activity activity, final Callback4<View> call) {
        getCoordinatorLayout(getActivityContentView(activity), call);
    }

    /**
     * 得到CoordinatorLayout
     */
    public static void getCoordinatorLayout(View view, final Callback4<View> call) {
        try {
            Class.forName("android.support.design.widget.CoordinatorLayout");
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException("CoordinatorLayout is null");
        }
        if (view == null) {
            return;
        }
        if (!isCoordinatorLayout(view)) {
            findCoordinatorLayout(view, call);
        } else {
            call.onYes(view);  //注册到onyes
        }
    }

    /**
     * 判断view是否是CoordinatorLayout类型的
     * CoordinatorLayout 请参照下面两个博客
     *
     * @param view activity的根目录
     */
    public static boolean isCoordinatorLayout(View view) {
        //http://blog.csdn.net/xyz_lmn/article/details/48055919 CoordinatorLayout的使用
        //http://blog.csdn.net/huachao1001/article/details/51554608
        return view instanceof CoordinatorLayout;
    }

    /**
     * 从根布局中依次查找 CoordinatorLayout 的view 找到便注册到callback4的onyes
     * 当中  不过不太理解将一个view 强转成viewgroup会不会多
     * 经过测试  这里的view是xml布局的最外层  group.getChildAt(i) 会依次得到里面的控件
     */
    private static void findCoordinatorLayout(View view, Callback4<View> mCall) {
        ViewGroup group = (ViewGroup) view;
        for (int i = 0; i < group.getChildCount(); i++) {
            View view1 = group.getChildAt(i);
            if (isCoordinatorLayout(view1)) {
                mCall.onYes(view1);
                return;
            }
        }
    }

    /**
     * 在activity中进行对象绑定的抽取
     *
     * @param activity 一个上下文对象
     * @param resource 一个xml的控件的id
     * @param <T> 一个继承view的泛型
     */
    @SuppressWarnings("unchecked")
    public static <T extends View> T findViewById(
            @NonNull Activity activity, @IdRes int resource) {
        return (T) activity.findViewById(resource);
    }

    @SuppressWarnings("unchecked")
    public static <T extends View> T findViewById(@NonNull View view,
            @IdRes int resource) {
        return (T) view.findViewById(resource);
    }

    public static <T extends View> T inflate(Context context, @LayoutRes int resource) {
        return inflate(context, resource, null);
    }

    public static <T extends View> T inflate(Context context, @LayoutRes int resource,
            ViewGroup root) {
        return inflate(context, resource, root, root != null);
    }

    @SuppressWarnings("unchecked")
    public static <T extends View> T inflate(Context context,
            @LayoutRes int resource,
            ViewGroup root,
            boolean attachToRoot) {
        return (T) LayoutInflater.from(context).inflate(resource, root, attachToRoot);
    }

    /**
     * 0    --------   VISIBLE    可见
     * 4    --------   INVISIBLE    不可见但是占用布局空间
     * 8    --------   GONE    不可见也不占用布局空间
     */
    public static boolean isVisible(View view) {
        return view != null && view.getVisibility() == View.VISIBLE;
    }

    /**
     * 控制view的显示和隐藏
     */
    public static void toggleVisibility(View view) {
        if (view == null) {
            return;
        }
        if (view.getVisibility() == View.GONE) {
            viewVisible(view);
        } else {
            viewGone(view);
        }
    }

    /**
     * 显示view
     */
    public static void viewVisible(View view) {
        if (view == null) {
            return;
        }
        view.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏view
     */
    public static void viewGone(View view) {
        if (view == null) {
            return;
        }
        view.setVisibility(View.GONE);
    }

    /**
     * view 隐藏但占空间
     */
    public static void viewInVisible(View view) {
        if (view == null) {
            return;
        }
        view.setVisibility(View.INVISIBLE);
    }

    /**
     * 添加一个view到viewGroup
     */
    public static void addView(@NonNull ViewGroup group, @NonNull View child) {
        group.removeAllViews();
        group.addView(child);
    }

}
